package top.kaoshanji.p1ch02d;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

/**
 * 代码清单 p1ch02d-13：MethodHandles#exactInvoker的使用示例
 * author kaoshanji
 * ^_^
 * create time 2025/1/3 20:01
 */
public class MethodHandlesExample13 {

    private static final Logger logger = LogManager.getLogger(MethodHandlesExample13.class);

    public static void main(String[] args) throws Throwable {
        logger.info("...代码清单 p1ch02d-13：MethodHandles#exactInvoker的使用示例...");

        MethodType typeInvoker = MethodType.methodType(String.class, String.class, int.class, int.class);
        MethodHandle invoker = MethodHandles.exactInvoker(typeInvoker);

        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle mhUpperCase = lookup.findVirtual(String.class, "toUpperCase", MethodType.methodType(String.class));
        invoker = MethodHandles.filterReturnValue(invoker, mhUpperCase);
        MethodType typeFind = MethodType.methodType(String.class, int.class, int.class);
        MethodHandle mh1 = lookup.findVirtual(String.class, "substring", typeFind);
        String result = (String) invoker.invoke(mh1, "Hello", 1, 4);

        System.out.println(result);
    }

}
